package com.back.service.impl;

import com.back.exception.BusinessException;
import com.back.exception.ErrorCode;
import com.back.exception.ThrowUtils;
import com.back.model.dto.hero.HeroAddRequest;
import com.back.model.dto.hero.HeroQueryRequest;
import com.back.model.dto.hero.HeroUpdateRequest;
import com.back.model.entity.Hero;
import com.back.model.vo.hero.HeroVO;
import com.back.service.HeroService;
import com.back.mapper.HeroMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 李瑞
* @description 针对表【Hero】的数据库操作Service实现
* @createDate 2025-09-12 09:23:53
*/
@Service
public class HeroServiceImpl extends ServiceImpl<HeroMapper, Hero>
    implements HeroService{

    @Resource
    private HeroMapper heroMapper;

    @Override
    public long addHero(HeroAddRequest heroAddRequest) {
        ThrowUtils.throwIf(heroAddRequest == null, ErrorCode.PARAMS_ERROR);
        
        Hero hero = new Hero();
        BeanUtils.copyProperties(heroAddRequest, hero);
        
        // 校验英雄信息
        validHero(hero, true);
        
        // 保存到数据库
        boolean result = this.save(hero);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        
        return hero.getId();
    }

    @Override
    public boolean deleteHero(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        
        // 判断是否存在
        Hero oldHero = this.getById(id);
        ThrowUtils.throwIf(oldHero == null, ErrorCode.NOT_FOUND_ERROR);
        
        // 删除
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean updateHero(HeroUpdateRequest heroUpdateRequest) {
        ThrowUtils.throwIf(heroUpdateRequest == null || heroUpdateRequest.getId() == null, ErrorCode.PARAMS_ERROR);
        
        Hero hero = new Hero();
        BeanUtils.copyProperties(heroUpdateRequest, hero);
        
        // 校验英雄信息
        validHero(hero, false);
        
        // 判断是否存在
        long id = heroUpdateRequest.getId();
        Hero oldHero = this.getById(id);
        ThrowUtils.throwIf(oldHero == null, ErrorCode.NOT_FOUND_ERROR);
        
        // 更新
        return this.updateById(hero);
    }

    @Override
    public HeroVO getHeroVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        
        Hero hero = this.getById(id);
        ThrowUtils.throwIf(hero == null, ErrorCode.NOT_FOUND_ERROR);
        
        return getHeroVO(hero);
    }

    @Override
    public IPage<HeroVO> listHeroVOByPage(HeroQueryRequest heroQueryRequest) {
        long current = heroQueryRequest.getCurrent();
        long size = heroQueryRequest.getPageSize();
        
        // 创建分页对象
        Page<Hero> heroPage = new Page<>(current, size);
        
        // 查询数据
        IPage<Hero> heroIPage = heroMapper.listHeroByPage(heroPage,
                heroQueryRequest.getHeroName(),
                heroQueryRequest.getTitle(),
                heroQueryRequest.getWeaponType(),
                heroQueryRequest.getAttributeType(),
                heroQueryRequest.getStarRingDivision(),
                heroQueryRequest.getDamageType());
        
        // 转换为VO
        IPage<HeroVO> heroVOIPage = new Page<>(current, size, heroIPage.getTotal());
        List<HeroVO> heroVOList = heroIPage.getRecords().stream()
                .map(this::getHeroVO)
                .collect(Collectors.toList());
        heroVOIPage.setRecords(heroVOList);
        
        return heroVOIPage;
    }

    @Override
    public List<HeroVO> getAllHeroVOList() {
        List<Hero> heroList = heroMapper.getAllHeroes();
        return heroList.stream()
                .map(this::getHeroVO)
                .collect(Collectors.toList());
    }

    @Override
    public void validHero(Hero hero, boolean add) {
        ThrowUtils.throwIf(hero == null, ErrorCode.PARAMS_ERROR);
        
        String heroName = hero.getHeroName();
        String title = hero.getTitle();
        String weaponType = hero.getWeaponType();
        String attributeType = hero.getAttributeType();
        String starRingDivision = hero.getStarRingDivision();
        String damageType = hero.getDamageType();
        String backgroundDescription = hero.getBackgroundDescription();
        String smallIconUrl = hero.getSmallIconUrl();
        String largeIconUrl = hero.getLargeIconUrl();
        String videoUrl = hero.getVideoUrl();
        
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(heroName), ErrorCode.PARAMS_ERROR, "英雄名称不能为空");
        }
        
        // 有参数则校验
        if (StringUtils.isNotBlank(heroName) && heroName.length() > 100) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "英雄名称过长");
        }
        if (StringUtils.isNotBlank(title) && title.length() > 100) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "称谓过长");
        }
        if (StringUtils.isNotBlank(weaponType) && weaponType.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器类型过长");
        }
        if (StringUtils.isNotBlank(attributeType) && attributeType.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "属性类型过长");
        }
        if (StringUtils.isNotBlank(starRingDivision) && starRingDivision.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "星之环分野过长");
        }
        if (StringUtils.isNotBlank(damageType) && damageType.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "伤害类型过长");
        }
        if (StringUtils.isNotBlank(smallIconUrl) && smallIconUrl.length() > 255) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "小图标URL过长");
        }
        if (StringUtils.isNotBlank(largeIconUrl) && largeIconUrl.length() > 255) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "大图标URL过长");
        }
        if (StringUtils.isNotBlank(videoUrl) && videoUrl.length() > 255) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "视频URL过长");
        }
    }

    @Override
    public HeroVO getHeroVO(Hero hero) {
        if (hero == null) {
            return null;
        }
        
        HeroVO heroVO = new HeroVO();
        BeanUtils.copyProperties(hero, heroVO);
        
        return heroVO;
    }

}




